package cn.lili.modules.promotion.manage;

import cn.lili.modules.promotion.entity.dos.ReturnActivity;
import cn.lili.modules.promotion.entity.enums.ReturnActivityTimeEnum;
import cn.lili.modules.promotion.service.ReturnActivityService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class ReturnActivityManage {

    @Resource
    private ReturnActivityService returnActivityService;

    /**
     * 解析金额，获取全返卷面额
     *
     * @param flowPrice 子订单号金额
     */
    public List<ReturnActivity> initReturnActivity(Integer flowPrice) {
        //查询可用全返卷面额集合
        List<ReturnActivity> returnActivityList = returnActivityService.getReturnActivity();
        //计算待返回的全返卷面额
        return render(returnActivityList, flowPrice);
    }

    /**
     * 计算全返卷面额
     * 有一个面额卷表为5块 3张  10块 2张  50块 2张...  用户购买商品如200块  则每次除以2进行返金额卷  金额卷向上取  张数没有了不取 取到0为止
     *
     * @param returnActivityList 可用全返卷面额集合
     * @param flowPrice          订单金额
     */
    public List<ReturnActivity> render(List<ReturnActivity> returnActivityList, Integer flowPrice) {
        AtomicInteger remainingAmount = new AtomicInteger(flowPrice);
        //待返回的全返卷面额
        List<ReturnActivity> resultList = new ArrayList<>();
        //待更新的全返卷面额
        List<ReturnActivity> updateList = new ArrayList<>();

        //计算
        returnActivityList.stream()
                .sorted((d1, d2) -> d2.getPrice().intValue() - d1.getPrice().intValue())
                .forEach(denomination -> {
                    int value = denomination.getPrice().intValue();

                    while (remainingAmount.get() >= value && denomination.getNumber() > 0) {
                        resultList.add(denomination);
                        remainingAmount.set(remainingAmount.get() - value);
                        denomination.setNumber(denomination.getNumber() - 1);
                        updateList.remove(denomination);
                        updateList.add(denomination);
                    }
                });

        returnActivityService.updateBatchById(updateList);
        return resultList;
    }

    /**
     * 根据ReturnActivityTimeEnum枚举，计算有效结束时间
     *
     * @param startDate 有效开始时间
     * @param duration  全返卷有效时间枚举
     * @return 有效结束时间
     */
    public LocalDate renderTime(LocalDate startDate, ReturnActivityTimeEnum duration) {
        return switch (duration) {
            case MONTH_ONE -> startDate.plusMonths(1);
            case MONTH_TWO -> startDate.plusMonths(2);
            case MONTH_THREE -> startDate.plusMonths(3);
            case MONTH_FOUR -> startDate.plusMonths(4);
            case MONTH_FIVE -> startDate.plusMonths(5);
            case MONTH_SIX -> startDate.plusMonths(6);
            case MONTH_SEVEN -> startDate.plusMonths(7);
            case MONTH_EIGHT -> startDate.plusMonths(8);
            case MONTH_NINE -> startDate.plusMonths(9);
            case MONTH_TEN -> startDate.plusMonths(10);
            case MONTH_ELEVEN -> startDate.plusMonths(11);
            case YEAR_ONE -> startDate.plusYears(1);
        };
    }
}
